package com.android.delivery.activity.wangdian

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import com.android.delivery.R
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseActivity
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.User
import com.android.delivery.util.ToastUtils
import com.blankj.utilcode.util.ObjectUtils
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_add_rider_management.*

/**
 * @author: mqwang
 * @create: 3/21/21 7:30 AM
 * @Description: 添加骑手
 */
class AddRiderManagementActivity : BaseActivity() {

    companion object {
        fun self(context: Activity, user: User, requestCode: Int) {
            val intent = Intent(context, AddRiderManagementActivity::class.java)
            intent.putExtra(Constants.ITEM, user)
            context.startActivityForResult(intent, requestCode)
        }

        fun self(context: Activity, requestCode: Int) {
            val intent = Intent(context, AddRiderManagementActivity::class.java)
            context.startActivityForResult(intent, requestCode)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_add_rider_management
    }

    override fun init(savedInstanceState: Bundle?, container: View?) {
        setMainTitle("骑手管理")

        val user = intent.getParcelableExtra<User>(Constants.ITEM)

        if (user != null) {
            et_name.setText(user.realName)
            et_phone.setText(user.mobile)
            et_jctc.setText((user.baseFeeRate!! * 100).toInt().toString())
            et_pjctc.setText((user.baseFeeDeliveryRate!! * 100).toInt().toString())
            et_cztc.setText((user.excessWeightFeeRate!! * 100).toInt().toString())
            et_qjcctc.setText((user.excessDistancePickFeeRate!! * 100).toInt().toString())
            et_pjcctc.setText((user.excessDistanceDeliveryFeeRate!! * 100).toInt().toString())
            et_pjfwtc.setText((user.serviceDeliveryFeeRate!! * 100).toInt().toString())
            et_xjtc.setText((user.excessQuantityRate!! * 100).toInt().toString())
            et_sjfl.setText((user.firstQuantityRate!! * 100).toInt().toString())
        }

        et_jctc.addTextChangedListener(NumTextWatcher(et_jctc))
        et_pjctc.addTextChangedListener(NumTextWatcher(et_pjctc))
        et_cztc.addTextChangedListener(NumTextWatcher(et_cztc))
        et_qjcctc.addTextChangedListener(NumTextWatcher(et_qjcctc))
        et_pjcctc.addTextChangedListener(NumTextWatcher(et_pjcctc))
        et_pjfwtc.addTextChangedListener(NumTextWatcher(et_pjfwtc))
        et_xjtc.addTextChangedListener(NumTextWatcher(et_xjtc))
        et_sjfl.addTextChangedListener(NumTextWatcher(et_sjfl))

        tv_confirm.setOnClickListener {
            var name = et_name.text.toString()
            var phone = et_phone.text.toString()
            var jctc = et_jctc.text.toString()
            var pjctc = et_pjctc.text.toString()
            var cztc = et_cztc.text.toString()
            var qjcctc = et_qjcctc.text.toString()
            var pjcctc = et_pjcctc.text.toString()
            var pjfwtc = et_pjfwtc.text.toString()
            var xjtc = et_xjtc.text.toString()
            var sjfl = et_sjfl.text.toString()

            if (ObjectUtils.isEmpty(name)) {
                ToastUtils.showShort(this, "请输入姓名")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(phone)) {
                ToastUtils.showShort(this, "请输入联系电话")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(jctc)) {
                ToastUtils.showShort(this, "请输入取件基础提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(pjctc)) {
                ToastUtils.showShort(this, "请输入派件基础提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(cztc)) {
                ToastUtils.showShort(this, "请输入超重提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(qjcctc)) {
                ToastUtils.showShort(this, "请输入派件超程提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(pjcctc)) {
                ToastUtils.showShort(this, "请输入取件超程提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(pjfwtc)) {
                ToastUtils.showShort(this, "请输入派件服务提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(xjtc)) {
                ToastUtils.showShort(this, "请输入续件提成")
                return@setOnClickListener
            }

            if (ObjectUtils.isEmpty(sjfl)) {
                ToastUtils.showShort(this, "请输入首件费率")
                return@setOnClickListener
            }


            ApiService.logistics_rider(this, if (user == null) "" else user.id, 0, name, phone, jctc.toDouble() / 100, pjctc.toDouble() / 100, cztc.toDouble() / 100, qjcctc.toDouble() / 100, pjcctc.toDouble() / 100, pjfwtc.toDouble() / 100, xjtc.toDouble() / 100, sjfl.toDouble() / 100, object : DialogCallback<LzyResponse<Any>>(this) {
                override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                    ToastUtils.showShort(this@AddRiderManagementActivity, "操作成功")
                    setResult(Activity.RESULT_OK)
                    finish()
                }
            })

        }
    }

    private class NumTextWatcher(val mEditText: EditText) : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
        override fun afterTextChanged(s: Editable) {
            val numberStr = s.toString()
            if (!TextUtils.isEmpty(numberStr)) {
                var number = numberStr.toInt()
                if (number > 100) {
                    mEditText.setText("100")
                    mEditText.setSelection("100".length)
                }
            }
        }
    }
}